#if !USE_CONSOLE

#include "src/interface/operationDialog.h"

#include <QtGui>
#include <iostream>

OperationDialog::OperationDialog(CombinedObject *_combinedObject, ObjectTreeItem *_item, QTreeWidgetItem *_root, GLWidget *_glWIdget, QWidget *_parent)
    : MaterialDialog(_glWIdget, _parent), combinedObject(_combinedObject), item(_item), root(_root){

    setWindowTitle(QString::fromUtf8("Modificar objeto ").append(QString::number(_combinedObject->getId())));

    const Material &material = combinedObject->getMaterial();
    ambientR = material.ambient.red * 255;
    ambientG = material.ambient.green * 255;
    ambientB = material.ambient.blue * 255;
    ambientA = material.ambient.alpha * 255;

    diffuseR = material.diffuse.red * 255;
    diffuseG = material.diffuse.green * 255;
    diffuseB = material.diffuse.blue * 255;
    diffuseA = material.diffuse.alpha * 255;

    specularR = material.specular.red * 255;
    specularG = material.specular.green * 255;
    specularB = material.specular.blue * 255;
    specularA = material.specular.alpha * 255;

    specularPower = material.specularPower;
    ka = material.ka;
    kd = material.kd;
    ks = material.ks;
    kt = material.kt;
    refractiveIndex = material.refractiveIndex;

    originalOperation = operation = combinedObject->getType();
    remove = false;


    Texture *texture = material.texture;
    if(texture)
    {
        textureFilePath = texture->textureImagePathName;
        textureRepeatTimesU = texture->repeatTimesU;
        textureRepeatTimesV = texture->repeatTimesV;
        textureColoringType = texture->coloringType;
        textureMappingType = texture->mappingType;
        useTexture = true;
    }
    else{
        textureFilePath = "";
        textureRepeatTimesU = textureRepeatTimesV = 5;
        textureColoringType = MULTIPLY;
        textureMappingType = ORIGINAL_TEXTURE;
        useTexture = false;
    }

    BumpMapping *bumpMapping = material.bumpMapping;
    if(bumpMapping)
    {
        bumpMappingFilePath = bumpMapping->bumpMappingImagePathName;
        bumpMappingRepeatTimesU = bumpMapping->repeatTimesU;
        bumpMappingRepeatTimesV = bumpMapping->repeatTimesV;
        bumpMappingMappingType = bumpMapping->mappingType;
        bumpMappingIntensity = bumpMapping->bumpIntensity;
        bumpMappingLoop =  bumpMapping->loop;
        useBumpMapping = true;
    }
    else{
        bumpMappingFilePath = "";
        bumpMappingRepeatTimesU = bumpMappingRepeatTimesV = 5;
        bumpMappingMappingType = ORIGINAL_TEXTURE;
        bumpMappingIntensity = 0.1;
        bumpMappingLoop =  true;
        useBumpMapping = false;
    }

    createLayout();
}

OperationDialog::~OperationDialog(){
}

void OperationDialog::createLayout(){


    QCheckBox *removeOperationCheckBox = new QCheckBox(QString::fromUtf8("Cancelar operação"));
    connect(removeOperationCheckBox, SIGNAL(clicked(bool)), this, SLOT(removeOperation(bool)));

    QComboBox *operationComboBox = new QComboBox();
    operationComboBox->addItem(QString::fromUtf8("União"));
    operationComboBox->addItem(QString::fromUtf8("Interseção"));
    operationComboBox->addItem(QString::fromUtf8("Diferença"));
    operationComboBox->setCurrentIndex(operation);
    connect(operationComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(changeOperation(int)));
//    connect(removeOperationCheckBox, SIGNAL(clicked(bool)), operationComboBox, SLOT(setDisabled(bool)));

    QHBoxLayout *operationLayout = new QHBoxLayout;
    operationLayout->addWidget(operationComboBox);
    operationLayout->addStretch(1);

    QGroupBox *operationGroupBox = new QGroupBox(QString::fromUtf8("Mudar de operação"));
    operationGroupBox->setLayout(operationLayout);
    connect(removeOperationCheckBox, SIGNAL(clicked(bool)), operationGroupBox, SLOT(setDisabled(bool)));



    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndChange()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QHBoxLayout *horizontalLayout = new QHBoxLayout;
    horizontalLayout->addWidget(operationGroupBox);
    horizontalLayout->addWidget(removeOperationCheckBox);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(horizontalLayout);

    createMaterialLayout(mainLayout);

    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void OperationDialog::acceptAndChange(){

    std::vector<Object *> objects;
    if(remove){
        QTreeWidgetItem *parent = item->parent();
        if(parent == NULL){
            parent = root;
//            std::cout <<"DEletando objeto sem pai" << std::endl;
            emit deleteObject(combinedObject);
        }else{
//            std::cout <<"DEletando objeto de pai " << ((ObjectTreeItem *)parent)->getObject()->getId() << std::endl;
            ((CombinedObject *)((ObjectTreeItem *)parent)->getObject())->deleteObject(combinedObject);
        }

//            std::cout <<"Adicionando de volta os filhos " << std::endl;
        for(int i = item->childCount(); i > 0; i--){
            QTreeWidgetItem *currentItem = item->child( i-1 );
            Object *currentObject = ((ObjectTreeItem *)currentItem)->getObject();
//            std::cout <<"\t\t1 "<< std::endl;
//            std::cout <<"\tfilho " << currentObject->getId() << std::endl;
            if(parent == root){
//            std::cout <<"\t\t2 "<< std::endl;
                emit addObject(currentObject);
                objects.push_back(currentObject);
            }else{
//            std::cout <<"\t\t3 "<< std::endl;
                ((CombinedObject *)((ObjectTreeItem *)parent)->getObject())->addObject(currentObject);
            }
//            std::cout <<"\t\t4 "<< std::endl;
            combinedObject->deleteObject(currentObject);

//            std::cout <<"\t\t5 "<< std::endl;
            item->removeChild(currentItem);
            parent->addChild(currentItem);
//            std::cout <<"\t\t6 "<< std::endl;
        }
//        std::cout <<"colocou todos os filhos" << std::endl;


        parent->removeChild(item);
        delete item;
        delete combinedObject;

//        std::cout <<"deletou de verdade" << std::endl;
        if(parent != root){
            objects.clear();
//            std::cout <<"\tlimpou a lista de objetos" << std::endl;
            while(parent->parent() != NULL){
                parent = parent->parent();
            }
//            std::cout <<"\tprocurando pai" << std::endl;
            objects.push_back(((ObjectTreeItem *)parent)->getObject());
        }

    }
    else{

        Texture *texture = NULL;
        if(useTexture)
        {
            texture = new Texture(glWidget, textureFilePath,
                                  textureRepeatTimesU, textureRepeatTimesV,
                                  textureColoringType, textureMappingType);
        }
        BumpMapping *bumpMapping = NULL;
        if(useBumpMapping)
        {
            bumpMapping = new BumpMapping(bumpMappingFilePath,
                                          bumpMappingRepeatTimesU, bumpMappingRepeatTimesV,
                                          bumpMappingMappingType, bumpMappingIntensity, bumpMappingLoop);
        }
        combinedObject->changeOperation(operation);
        combinedObject->setMaterial(Material(Color(ambientR, ambientG, ambientB, ambientA)/255.0, Color(diffuseR, diffuseG, diffuseB, diffuseA)/255.0, Color(specularR, specularG, specularB, specularA)/255.0, specularPower, ka, kd, ks, kt, refractiveIndex, texture, bumpMapping));
        item->update();
        objects.push_back(combinedObject);
    }

    if(originalOperation != operation){
        emit changed();
    }

    accept();
    done();
}

void OperationDialog::changeOperation(int _index){
    operation = _index;
}

void OperationDialog::removeOperation(bool _b){
    remove = _b;
}
#endif //!USE_CONSOLE
